package com.example.twitterfake;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class Rest {

	public static String login(String login, String senha) throws ClientProtocolException, IOException{
		final HttpParams httpParams = new BasicHttpParams();
	    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
	    HttpConnectionParams.setSoTimeout(httpParams, 5000);
		HttpClient client = new DefaultHttpClient(httpParams);
		HttpPost post = new HttpPost("http://192.168.0.102:3000/login.json");
  
        List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("user[username]", login));
        nameValuePairs.add(new BasicNameValuePair("user[password]", senha));
        post.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
  
        HttpResponse response = client.execute(post);
        
        
        
        
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = rd.readLine();
        
		return line;
    
	}
	
	public static String getTrendTops ()  throws ClientProtocolException, IOException {
		final HttpParams httpParams = new BasicHttpParams();
	    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
	    HttpConnectionParams.setSoTimeout(httpParams, 5000);
		HttpClient client = new DefaultHttpClient(httpParams);
		HttpGet get = new HttpGet("http://192.168.0.100:3000/trending_topics.json");
		
		HttpResponse response = client.execute(get);
		 
	    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	    
	    String trends = rd.readLine();
	    
	    return trends;	
	}
	
	public static String getPostsId (String id) throws ClientProtocolException, IOException{
		final HttpParams httpParams = new BasicHttpParams();
	    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
	    HttpConnectionParams.setSoTimeout(httpParams, 5000);
		HttpClient client = new DefaultHttpClient(httpParams);
		HttpGet get = new HttpGet("http://192.168.0.100:3000/posts/"+id+".json");

        HttpResponse response = client.execute(get);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = rd.readLine ();
		return line;		
	}
	
}
